package bb.lanxing.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.commons.cli.HelpFormatter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.mikephil.charting.utils.Utils;

import android.util.SparseArray;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.calc.data.DisplayPoint;
import bb.lanxing.lib.devices.sprint.entity.SettingSupports;
import bb.lanxing.model.database.Workout;
import bb.lanxing.model.device.IDevicePanelItem;
import bb.lanxing.model.sport.ISportItem;
import bb.lanxing.util.CommonUtil;

public class PanelItemTypeUtil {
    public static final int TYPE_AUTO_DASHBOARD = 100;
    public static final int TYPE_CLASSIC = 1;
    public static final int TYPE_RUNNING = 3;
    public static final int TYPE_WALKING = 4;

    private static final String PROFILE_NAME = "sport_watch_face_new_%d.profile";
    private static final String DEFAULT_FLOAT_1 = "0.0";
    private static final String DEFAULT_FLOAT_2 = "0.00";
    private static final String DEFAULT_INT = "0";
    private static final String DEFAULT_PACE = "0'0\"";
    private static final String DEFAULT_TIME = "00:00";
    private static DecimalFormat decimalFormat = new DecimalFormat(DEFAULT_INT);

    public static List<Integer> loadCache(long userId, int sportType) {
        return loadCacheAll(userId).get(sportType);
    }

    private static SparseArray<List<Integer>> loadCacheAll(long userId) {
        File profileFile =
                new File(App.getContext().getCacheDir(), String.format(Locale.ENGLISH, PROFILE_NAME, userId));
        if (profileFile.exists()) {
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new FileReader(profileFile));
                StringBuilder sb = new StringBuilder();
                while (true) {
                    String readLine = bufferedReader.readLine();
                    if (readLine == null) {
                        break;
                    }
                    sb.append(readLine);
                }
                bufferedReader.close();
                SparseArray<List<Integer>> parseJson = parseJson(sb.toString());
                return parseJson;
            } catch (IOException e) {
                e.printStackTrace();
                if (bufferedReader != null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
                return generateDefault();
            }
        }
        return generateDefault();
    }

    private static SparseArray<List<Integer>> parseJson(String json) {
        JSONArray jsonList = JSON.parseArray(json);
        SparseArray<List<Integer>> sparseArray = new SparseArray<>(jsonList.size());
        for (int i = 0; i < jsonList.size(); i++) {
            JSONObject jsonOne = jsonList.getJSONObject(i);
            int sport = jsonOne.getIntValue(SettingSupports.SUPPORT_ITEM_SPORT);
            JSONArray sport_types = jsonOne.getJSONArray("dashboards").getJSONObject(0).getJSONArray("sport_types");
            List<Integer> panelItemTypeList = new ArrayList<>();
            for (int i3 = 0; i3 < sport_types.size(); i3++) {
                panelItemTypeList.add((Integer) sport_types.get(i3));
            }
            sparseArray.put(sport, panelItemTypeList);
        }
        return sparseArray;
    }

    private static SparseArray<List<Integer>> generateDefault() {
        SparseArray<List<Integer>> sparseArray = new SparseArray<>();
        sparseArray.put(/* 3 */Workout.TYPE_CYCLING, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 2 */Workout.TYPE_RUNNING, newDefaultPanelItemTypeList(TYPE_RUNNING));
        sparseArray.put(/* 1 */Workout.TYPE_WALKING, newDefaultPanelItemTypeList(TYPE_WALKING));
        sparseArray.put(/* 6 */Workout.TYPE_SKIING, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 5 */Workout.TYPE_SWIMMING, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 8 */Workout.TYPE_TRAINING, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 0 */Workout.TYPE_OTHER, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 11 */Workout.TYPE_INDOOR_BIKE, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 13 */Workout.TYPE_ROWER, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 14 */Workout.TYPE_CROSS_TRAINER, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        sparseArray.put(/* 15 */Workout.TYPE_BIKE_TRAINER, newDefaultPanelItemTypeList(TYPE_CLASSIC));
        return sparseArray;
    }

    private static List<Integer> newDefaultPanelItemTypeList(int themeType) {
        List<Integer> panelItemTypeList = new ArrayList();
        if (themeType == TYPE_RUNNING) {
            panelItemTypeList.add(ISportItem.DATA_TYPE_PACE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_DISTANCE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_DURATION);
            panelItemTypeList.add(ISportItem.DATA_TYPE_AVG_PACE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_MAX_PACE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_STEP);
            panelItemTypeList.add(ISportItem.DATA_TYPE_CALORIE);
        } else if (themeType == TYPE_WALKING) {
            panelItemTypeList.add(ISportItem.DATA_TYPE_DISTANCE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_ALTITUDE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_DURATION);
            panelItemTypeList.add(ISportItem.DATA_TYPE_SPEED);
            panelItemTypeList.add(ISportItem.DATA_TYPE_AVG_SPEED);
            panelItemTypeList.add(ISportItem.DATA_TYPE_STEP);
            panelItemTypeList.add(ISportItem.DATA_TYPE_CALORIE);
        } else if (themeType == TYPE_AUTO_DASHBOARD) {
            panelItemTypeList.add(ISportItem.DATA_TYPE_SPEED);
            panelItemTypeList.add(ISportItem.DATA_TYPE_DISTANCE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_DURATION);
            panelItemTypeList.add(ISportItem.DATA_TYPE_ALTITUDE);
        } else {
            panelItemTypeList.add(ISportItem.DATA_TYPE_PACE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_DISTANCE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_DURATION);
            panelItemTypeList.add(ISportItem.DATA_TYPE_AVG_SPEED);
            panelItemTypeList.add(ISportItem.DATA_TYPE_MAX_SPEED);
            panelItemTypeList.add(ISportItem.DATA_TYPE_ALTITUDE);
            panelItemTypeList.add(ISportItem.DATA_TYPE_ELEVATION_GAIN);
        }
        return panelItemTypeList;
    }

    public static String getItemName(int i) {
        int i2;
        int i3 = R.string.unit_hots;
        int i4 = 0;
        switch (i) {
            case ISportItem.DATA_TYPE_DISTANCE -> {
                i2 = R.string.sport_tag_distance;
                i3 = R.string.unit_km;
            }
            case ISportItem.DATA_TYPE_SPEED -> {
                i2 = R.string.sport_tag_speed;
                i3 = R.string.unit_km_h;
            }
            case ISportItem.DATA_TYPE_AVG_SPEED -> {
                i2 = R.string.sport_tag_avg_speed;
                i3 = R.string.unit_km_h;
            }
            case ISportItem.DATA_TYPE_MAX_SPEED -> {
                i2 = R.string.sport_tag_max_speed;
                i3 = R.string.unit_km_h;
            }
            case ISportItem.DATA_TYPE_AVG_SPEED_TOTAL_TIME -> {
                i2 = R.string.sport_tag_avg_speed_total_time;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_DURATION -> {
                i2 = R.string.sport_tag_duration;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_TOTAL_DURATION -> {
                i2 = R.string.sport_tag_duration_total;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_PAUSE_DURATION -> {
                i2 = R.string.sport_tag_pause_time;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_PACE -> {
                i2 = R.string.sport_tag_pace;
                i3 = R.string.unit_min_km;
            }
            case ISportItem.DATA_TYPE_AVG_PACE -> {
                i2 = R.string.sport_tag_avg_pace;
                i3 = R.string.unit_min_km;
            }
            case ISportItem.DATA_TYPE_MAX_PACE -> {
                i2 = R.string.sport_tag_max_pace;
                i3 = R.string.unit_min_km;
            }
            case ISportItem.DATA_TYPE_ALTITUDE -> {
                i2 = R.string.sport_tag_altitude;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_GRADIENT -> {
                i2 = R.string.sport_tag_gradient;
                i3 = R.string.unit_percent;
            }
            case ISportItem.DATA_TYPE_ELEVATION_GAIN -> {
                i2 = R.string.sport_tag_elevation_gain;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_AVG_ELEVATION_GAIN -> {
                i2 = R.string.sport_tag_avg_elevation_gain;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_AVG_ELEVATION_LOSS -> {
                i2 = R.string.sport_tag_avg_elevation_lost;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_MAX_ELEVATION_GAIN -> {
                i2 = R.string.sport_tag_max_elevation_gain;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_MAX_ELEVATION_LOSS -> {
                i2 = R.string.sport_tag_max_elevation_lost;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_CADENCE -> {
                i2 = R.string.sport_tag_cadence;
                i3 = 0;
                i4 = R.drawable.ic_sport_dashboard_cadence;
            }
            case ISportItem.DATA_TYPE_AVG_CADENCE -> {
                i2 = R.string.sport_tag_avg_cadence;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_MAX_CADENCE -> {
                i2 = R.string.sport_tag_max_cadence;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_HEART_RATE -> {
                i2 = R.string.sport_tag_heartrate;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_AVG_HEART_RATE -> {
                i2 = R.string.sport_tag_avg_hr;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_MAX_HEART_RATE -> {
                i2 = R.string.sport_tag_max_hr;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_MAX_HEART_RATE_PERCENTAGE -> {
                i2 = R.string.sport_tag_max_hr_percentage;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_HEART_RATE_LTHT -> {
                i2 = R.string.sport_tag_lthr;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_POWER -> {
                i2 = R.string.sport_tag_power;
                i3 = R.string.unit_power;
            }
            case ISportItem.DATA_TYPE_AVG_POWER -> {
                i2 = R.string.sport_tag_avg_power;
                i3 = R.string.unit_power;
            }
            case ISportItem.DATA_TYPE_MAX_POWER -> {
                i2 = R.string.sport_tag_max_power;
                i3 = R.string.unit_power;
            }
            case ISportItem.DATA_TYPE_POWER_3_SECONDS -> {
                i2 = R.string.sport_tag_power_3_seconds;
                i3 = R.string.unit_power;
            }
            case ISportItem.DATA_TYPE_POWER_10_SECONDS -> {
                i2 = R.string.sport_tag_power_10_seconds;
                i3 = R.string.unit_power;
            }
            case ISportItem.DATA_TYPE_POWER_30_SECONDS -> {
                i2 = R.string.sport_tag_power_30_seconds;
                i3 = R.string.unit_power;
            }
            case ISportItem.DATA_TYPE_FTP_PERCENTAGE -> {
                i2 = R.string.sport_tag_ftp_percentage;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_POWER_NP -> {
                i2 = R.string.sport_tag_np;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_POWER_TSS -> {
                i2 = R.string.sport_tag_tss;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_TEMPERATURE -> i2 = R.string.sport_tag_current_temperature;
            case ISportItem.DATA_TYPE_MAX_TEMPERATURE -> i2 = R.string.sport_tag_max_temperature;
            case ISportItem.DATA_TYPE_MIN_TEMPERATURE -> i2 = R.string.sport_tag_min_temperature;
            case ISportItem.DATA_TYPE_CALORIE -> {
                i2 = R.string.sport_tag_calorie;
                i3 = R.string.unit_kcal;
            }
            case ISportItem.DATA_TYPE_STEP -> {
                i2 = R.string.sport_tag_step;
                i3 = 0;
                i4 = R.drawable.ic_sport_dashboard_power;
            }
            case ISportItem.DATA_TYPE_CURRENT_DIRECTION -> {
                i2 = R.string.sport_tag_current_direction;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_CURRENT_AZIMUTH -> {
                i2 = R.string.sport_tag_current_azimuth;
                i3 = 0;
            }
            case ISportItem.DATA_TYPE_MAX_ALTITUDE -> {
                i2 = R.string.sport_tag_max_altitude;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_MIN_ALTITUDE -> {
                i2 = R.string.sport_tag_min_altitude;
                i3 = R.string.unit_m;
            }
            case ISportItem.DATA_TYPE_ELEVATION_LOSS -> {
                i2 = R.string.sport_tag_elevation_lost;
                i3 = R.string.unit_m;
            }
            default -> {
                i2 = 0;
                i3 = 0;
            }
        }
        String name = App.getContext().getString(i2);
        if (i3 != 0) {
            name += "(" + App.getContext().getString(i3) + ")";
        }
        return name;
    }

    public static String getItemValue(DisplayPoint displayPoint, int itemType) {
        init(itemType);
        if (displayPoint == null) {
            return resetDefault(itemType);
        }
        String str = HelpFormatter.DEFAULT_OPT_PREFIX;
        long j = 0;
        int i2 = 0;
        float f = 0.0f;
        double d = Utils.DOUBLE_EPSILON;
        String value;
        switch (itemType) {
            case ISportItem.DATA_TYPE_DISTANCE -> {
                value = decimalFormat.format(displayPoint.getDistance(Utils.DOUBLE_EPSILON) / 1000.0d);
            }
            case ISportItem.DATA_TYPE_SPEED -> {
                value = decimalFormat.format(displayPoint.getSpeed(Utils.DOUBLE_EPSILON) * 3.6d);
            }
            case ISportItem.DATA_TYPE_AVG_SPEED -> {
                value = decimalFormat.format(displayPoint.getAvgSpeed(Utils.DOUBLE_EPSILON) * 3.6d);
            }
            case ISportItem.DATA_TYPE_MAX_SPEED -> {
                value = decimalFormat.format(displayPoint.getMaxSpeed(Utils.DOUBLE_EPSILON) * 3.6d);
            }
            case ISportItem.DATA_TYPE_AVG_SPEED_TOTAL_TIME -> {
                Double totalAvgSpeed = displayPoint.getTotalAvgSpeed();
                if (totalAvgSpeed != null) {
                    d = totalAvgSpeed * 3.6d;
                }
                value = decimalFormat.format(d);
            }
            case ISportItem.DATA_TYPE_DURATION -> {
                value = DateUtil.format(displayPoint.getDuration(0L) * 1000, 2);
            }
            case ISportItem.DATA_TYPE_TOTAL_DURATION -> {
                Long totalDuration = displayPoint.getTotalDuration();
                if (totalDuration != null) {
                    j = totalDuration * 1000;
                }
                value = DateUtil.format(j, 2);
            }
            case ISportItem.DATA_TYPE_PAUSE_DURATION -> {
                Long pauseDuration = displayPoint.getPauseDuration();
                if (pauseDuration != null) {
                    j = pauseDuration * 1000;
                }
                value = DateUtil.format(j, 2);
            }
            case ISportItem.DATA_TYPE_PACE -> {
                value = CommonUtil.formatPace(displayPoint.getSpeed(Utils.DOUBLE_EPSILON));
            }
            case ISportItem.DATA_TYPE_AVG_PACE -> {
                value = CommonUtil.formatPace(displayPoint.getAvgSpeed(Utils.DOUBLE_EPSILON));
            }
            case ISportItem.DATA_TYPE_MAX_PACE -> {
                value = CommonUtil.formatPace(displayPoint.getMaxSpeed(Utils.DOUBLE_EPSILON));
            }
            case ISportItem.DATA_TYPE_ALTITUDE -> {
                value = decimalFormat.format(displayPoint.getAltitude(Utils.DOUBLE_EPSILON));
            }
            case ISportItem.DATA_TYPE_GRADIENT -> {
                value = String.format(Locale.getDefault(), "%.1f", displayPoint.getGrade(0.0f));
            }
            case ISportItem.DATA_TYPE_ELEVATION_GAIN -> {
                value = decimalFormat.format(displayPoint.getElevationGain(0.0f));
            }
            case ISportItem.DATA_TYPE_AVG_ELEVATION_GAIN -> {
                Float avgElevationGain = displayPoint.getAvgElevationGain();
                DecimalFormat decimalFormat2 = decimalFormat;
                if (avgElevationGain != null) {
                    d = avgElevationGain;
                }
                value = decimalFormat2.format(d);
            }
            case ISportItem.DATA_TYPE_AVG_ELEVATION_LOSS -> {
                Float avgElevationLoss = displayPoint.getAvgElevationLoss();
                DecimalFormat decimalFormat3 = decimalFormat;
                if (avgElevationLoss != null) {
                    d = avgElevationLoss;
                }
                value = decimalFormat3.format(d);
            }
            case ISportItem.DATA_TYPE_MAX_ELEVATION_GAIN -> {
                Float maxElevationGain = displayPoint.getMaxElevationGain();
                DecimalFormat decimalFormat4 = decimalFormat;
                if (maxElevationGain != null) {
                    d = maxElevationGain;
                }
                value = decimalFormat4.format(d);
            }
            case ISportItem.DATA_TYPE_MAX_ELEVATION_LOSS -> {
                Float maxElevationLoss = displayPoint.getMaxElevationLoss();
                DecimalFormat decimalFormat5 = decimalFormat;
                if (maxElevationLoss != null) {
                    d = maxElevationLoss;
                }
                value = decimalFormat5.format(d);
            }
            case ISportItem.DATA_TYPE_CADENCE -> {
                value = String.valueOf(displayPoint.getCadence(0));
            }
            case ISportItem.DATA_TYPE_AVG_CADENCE -> {
                Integer avgCadence = displayPoint.getAvgCadence();
                if (avgCadence != null) {
                    i2 = avgCadence;
                }
                value = String.valueOf(i2);
            }
            case ISportItem.DATA_TYPE_MAX_CADENCE -> {
                Integer maxCadence = displayPoint.getMaxCadence();
                if (maxCadence != null) {
                    i2 = maxCadence;
                }
                value = String.valueOf(i2);
            }
            case ISportItem.DATA_TYPE_HEART_RATE -> {
                value = String.valueOf(displayPoint.getHeartrate(0));
            }
            case ISportItem.DATA_TYPE_AVG_HEART_RATE -> {
                Integer avgHeartrate = displayPoint.getAvgHeartrate();
                if (avgHeartrate != null) {
                    i2 = avgHeartrate;
                }
                value = String.valueOf(i2);
            }
            case ISportItem.DATA_TYPE_MAX_HEART_RATE -> {
                Integer maxHeartrate = displayPoint.getMaxHeartrate();
                if (maxHeartrate != null) {
                    i2 = maxHeartrate;
                }
                value = String.valueOf(i2);
            }
            case ISportItem.DATA_TYPE_MAX_HEART_RATE_PERCENTAGE -> {
                Float maxHeartratePer = displayPoint.getMaxHeartratePer();
                Locale locale = Locale.getDefault();
                Object[] objArr = new Object[1];
                if (maxHeartratePer != null) {
                    f = maxHeartratePer * 100.0f;
                }
                objArr[0] = f;
                value = String.format(locale, "%.1f", objArr);
            }
            case ISportItem.DATA_TYPE_HEART_RATE_LTHT -> {
                Float heartrateLTHT = displayPoint.getHeartrateLTHT();
                DecimalFormat decimalFormat6 = decimalFormat;
                if (heartrateLTHT != null) {
                    d = heartrateLTHT;
                }
                value = decimalFormat6.format(d);
            }
            case ISportItem.DATA_TYPE_POWER -> {
                value = String.valueOf(Math.round(displayPoint.getPower(0.0f)));
            }
            case ISportItem.DATA_TYPE_AVG_POWER -> {
                Float avgPower = displayPoint.getAvgPower();
                if (avgPower != null) {
                    f = avgPower;
                }
                value = String.valueOf(Math.round(f));
            }
            case ISportItem.DATA_TYPE_MAX_POWER -> {
                Float maxPower = displayPoint.getMaxPower();
                if (maxPower != null) {
                    f = maxPower;
                }
                value = String.valueOf(Math.round(f));
            }
            case ISportItem.DATA_TYPE_POWER_3_SECONDS -> {
                Float avg3sPower = displayPoint.getAvg3sPower();
                if (avg3sPower != null) {
                    f = avg3sPower;
                }
                value = String.valueOf(Math.round(f));
            }
            case ISportItem.DATA_TYPE_POWER_10_SECONDS -> {
                Float avg10sPower = displayPoint.getAvg10sPower();
                if (avg10sPower != null) {
                    f = avg10sPower;
                }
                value = String.valueOf(Math.round(f));
            }
            case ISportItem.DATA_TYPE_POWER_30_SECONDS -> {
                Float avg30sPower = displayPoint.getAvg30sPower();
                if (avg30sPower != null) {
                    f = avg30sPower;
                }
                value = String.valueOf(Math.round(f));
            }
            case ISportItem.DATA_TYPE_FTP_PERCENTAGE -> {
                Float powerFTPPer = displayPoint.getPowerFTPPer();
                Locale locale2 = Locale.getDefault();
                Object[] objArr2 = new Object[1];
                if (powerFTPPer != null) {
                    f = powerFTPPer * 100.0f;
                }
                objArr2[0] = f;
                value = String.format(locale2, "%.1f", objArr2);
            }
            case ISportItem.DATA_TYPE_POWER_NP -> {
                Float powerNP = displayPoint.getPowerNP();
                DecimalFormat decimalFormat7 = decimalFormat;
                if (powerNP != null) {
                    d = powerNP;
                }
                value = decimalFormat7.format(d);
            }
            case ISportItem.DATA_TYPE_POWER_TSS -> {
                Float powerTSS = displayPoint.getPowerTSS();
                if (powerTSS != null) {
                    f = powerTSS;
                }
                value = String.valueOf(Math.round(f));
            }
            case ISportItem.DATA_TYPE_TEMPERATURE -> {
                value = decimalFormat.format(displayPoint.getTemperature(0.0f));
            }
            case ISportItem.DATA_TYPE_MAX_TEMPERATURE -> {
                Float maxTemperature = displayPoint.getMaxTemperature();
                DecimalFormat decimalFormat8 = decimalFormat;
                if (maxTemperature != null) {
                    d = maxTemperature;
                }
                value = decimalFormat8.format(d);
            }
            case ISportItem.DATA_TYPE_MIN_TEMPERATURE -> {
                Float minTemperature = displayPoint.getMinTemperature();
                DecimalFormat decimalFormat9 = decimalFormat;
                if (minTemperature != null) {
                    d = minTemperature;
                }
                value = decimalFormat9.format(d);
            }
            case ISportItem.DATA_TYPE_CALORIE -> {
                value = String.valueOf(displayPoint.getCalorie(0) / 1000);
            }
            case ISportItem.DATA_TYPE_STEP -> {
                value = String.valueOf(displayPoint.getSteps(0));
            }
            case ISportItem.DATA_TYPE_CURRENT_DIRECTION -> {
                /*
                 * Float direction = displayPoint.getDirection(); Locale locale3 = Locale.getDefault(); Object[] objArr3 =
                 * new Object[1]; if (direction != null) { f = direction; } objArr3[0] = f; value = String.format(locale3,
                 * "%.1f°", objArr3);
                 */
                Integer InstantAzimuth = displayPoint.getAzimuth();
                if (InstantAzimuth != null) {
                    if (InstantAzimuth == 0) {
                        value = App.getContext().getString(R.string.direction_north);
                    } else if (InstantAzimuth == 45) {
                        value = App.getContext().getString(R.string.direction_northeast);
                    } else if (InstantAzimuth == 90) {
                        value = App.getContext().getString(R.string.direction_east);
                    } else if (InstantAzimuth == 135) {
                        value = App.getContext().getString(R.string.direction_southeast);
                    } else if (InstantAzimuth == 180) {
                        value = App.getContext().getString(R.string.direction_south);
                    } else if (InstantAzimuth == 225) {
                        value = App.getContext().getString(R.string.direction_southwest);
                    } else if (InstantAzimuth == 270) {
                        value = App.getContext().getString(R.string.direction_west);
                    } else if (InstantAzimuth == 315) {
                        value = App.getContext().getString(R.string.direction_northwest);
                    } else if (InstantAzimuth == 360) {
                        value = App.getContext().getString(R.string.direction_north);
                    } else if ((InstantAzimuth > 0) && (InstantAzimuth < 90)) {
                        value = App.getContext().getString(R.string.direction_northbyeast) + InstantAzimuth
                                + App.getContext().getString(R.string.direction_degree);
                    } else if ((InstantAzimuth > 90) && (InstantAzimuth < 180)) {
                        value = App.getContext().getString(R.string.direction_southbyeast) + (180 - InstantAzimuth)
                                + App.getContext().getString(R.string.direction_degree);
                    } else if ((InstantAzimuth > 180) && (InstantAzimuth < 270)) {
                        value = App.getContext().getString(R.string.direction_southbywest) + (InstantAzimuth - 180)
                                + App.getContext().getString(R.string.direction_degree);
                    } else if ((InstantAzimuth > 270) && (InstantAzimuth < 360)) {
                        value = App.getContext().getString(R.string.direction_northbywest) + (360 - InstantAzimuth)
                                + App.getContext().getString(R.string.direction_degree);
                    } else {
                        value = "0";
                    }
                } else {
                    str = "0";
                    value = str;
                }
            }
            case ISportItem.DATA_TYPE_CURRENT_AZIMUTH -> {
                Integer CurrentAzimuth = displayPoint.getAzimuth();
                if (CurrentAzimuth != null) {
                    value = CurrentAzimuth + App.getContext().getString(R.string.direction_degree);
                } else {
                    str = "0";
                    value = str;
                }
            }
            case ISportItem.DATA_TYPE_MAX_ALTITUDE -> {
                Double maxAltitude = displayPoint.getMaxAltitude();
                DecimalFormat decimalFormat10 = decimalFormat;
                if (maxAltitude != null) {
                    d = maxAltitude;
                }
                value = decimalFormat10.format(d);
            }
            case ISportItem.DATA_TYPE_MIN_ALTITUDE -> {
                Double minAltitude = displayPoint.getMinAltitude();
                DecimalFormat decimalFormat11 = decimalFormat;
                if (minAltitude != null) {
                    d = minAltitude;
                }
                value = decimalFormat11.format(d);
            }
            case ISportItem.DATA_TYPE_ELEVATION_LOSS -> {
                Float elevationLoss = displayPoint.getElevationLoss();
                DecimalFormat decimalFormat12 = decimalFormat;
                if (elevationLoss != null) {
                    d = elevationLoss;
                }
                value = decimalFormat12.format(d);
            }
            default -> {
                value = str;
            }
        }
        return value;
    }

    private static void init(int dataType) {
        switch (dataType) {
            case ISportItem.DATA_TYPE_DISTANCE, ISportItem.DATA_TYPE_SPEED,
                 ISportItem.DATA_TYPE_AVG_SPEED,
                 ISportItem.DATA_TYPE_MAX_SPEED, ISportItem.DATA_TYPE_AVG_SPEED_TOTAL_TIME ->
                    decimalFormat = new DecimalFormat(DEFAULT_FLOAT_2);
            case ISportItem.DATA_TYPE_ALTITUDE, ISportItem.DATA_TYPE_GRADIENT,
                 ISportItem.DATA_TYPE_ELEVATION_GAIN,
                 ISportItem.DATA_TYPE_AVG_ELEVATION_GAIN, ISportItem.DATA_TYPE_AVG_ELEVATION_LOSS,
                 ISportItem.DATA_TYPE_MAX_ELEVATION_GAIN, ISportItem.DATA_TYPE_MAX_ELEVATION_LOSS,
                 ISportItem.DATA_TYPE_HEART_RATE_LTHT, ISportItem.DATA_TYPE_POWER_NP,
                 ISportItem.DATA_TYPE_TEMPERATURE,
                 ISportItem.DATA_TYPE_MAX_TEMPERATURE, ISportItem.DATA_TYPE_MIN_TEMPERATURE,
                 ISportItem.DATA_TYPE_MAX_ALTITUDE, ISportItem.DATA_TYPE_MIN_ALTITUDE,
                 ISportItem.DATA_TYPE_ELEVATION_LOSS ->
                    decimalFormat = new DecimalFormat(DEFAULT_FLOAT_1);
            default -> {
            }
        }
    }

    public static String resetDefault(int dataType) {
        String value;
        switch (dataType) {
            case ISportItem.DATA_TYPE_DISTANCE, ISportItem.DATA_TYPE_SPEED,
                 ISportItem.DATA_TYPE_AVG_SPEED,
                 ISportItem.DATA_TYPE_MAX_SPEED, ISportItem.DATA_TYPE_AVG_SPEED_TOTAL_TIME ->
                    value = DEFAULT_FLOAT_2;
            case ISportItem.DATA_TYPE_DURATION, ISportItem.DATA_TYPE_TOTAL_DURATION,
                 ISportItem.DATA_TYPE_PAUSE_DURATION -> value = DEFAULT_TIME;
            case ISportItem.DATA_TYPE_PACE, ISportItem.DATA_TYPE_AVG_PACE,
                 ISportItem.DATA_TYPE_MAX_PACE -> value = DEFAULT_PACE;
            case ISportItem.DATA_TYPE_ALTITUDE, ISportItem.DATA_TYPE_GRADIENT,
                 ISportItem.DATA_TYPE_ELEVATION_GAIN,
                 ISportItem.DATA_TYPE_AVG_ELEVATION_GAIN, ISportItem.DATA_TYPE_AVG_ELEVATION_LOSS,
                 ISportItem.DATA_TYPE_MAX_ELEVATION_GAIN, ISportItem.DATA_TYPE_MAX_ELEVATION_LOSS,
                 ISportItem.DATA_TYPE_POWER_NP, ISportItem.DATA_TYPE_MAX_ALTITUDE,
                 ISportItem.DATA_TYPE_MIN_ALTITUDE,
                 ISportItem.DATA_TYPE_ELEVATION_LOSS -> value = DEFAULT_FLOAT_1;
            case ISportItem.DATA_TYPE_CURRENT_DIRECTION, ISportItem.DATA_TYPE_CURRENT_AZIMUTH ->
                    value = HelpFormatter.DEFAULT_OPT_PREFIX;
            default -> value = "0";
        }
        return value;
    }

    public static void savePanelSetting(final long userId, final int sportType, final List<Integer> panelSettings) {
        SparseArray<List<Integer>> sparseArray = loadCacheAll(userId);
        for (int i = 0; i < sparseArray.size(); i++) {
            int keyAt = sparseArray.keyAt(i);
            if (keyAt == sportType) {
                sparseArray.set(keyAt, panelSettings);
                break;
            }
        }
        saveProfile(toJson(sparseArray), userId);
    }

    private static boolean saveProfile(String saveString, long userId) {
        File profileFile =
                new File(App.getContext().getCacheDir(), String.format(Locale.ENGLISH, PROFILE_NAME, userId));
        try {
            if (!profileFile.exists() && !profileFile.createNewFile()) {
                throw new IllegalStateException("cannot create profile file !");
            }
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(profileFile, false));
            bufferedWriter.write(saveString, 0, saveString.length());
            bufferedWriter.flush();
            bufferedWriter.close();
            return true;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static String toJson(SparseArray<List<Integer>> sportTypesMap) {
        JSONArray jSONArray = new JSONArray();
        for (int i = 0; i < sportTypesMap.size(); i++) {
            int keyAt = sportTypesMap.keyAt(i);
            JSONObject jSONObject = new JSONObject();
            jSONObject.put(SettingSupports.SUPPORT_ITEM_SPORT, keyAt);
            List<Integer> list = sportTypesMap.get(keyAt);
            JSONArray jSONArray2 = new JSONArray();
            int[] iArr = new int[list.size()];
            for (int i2 = 0; i2 < list.size(); i2++) {
                iArr[i2] = list.get(i2);
            }
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("sport_types", (Object) iArr);
            jSONArray2.add(jSONObject2);
            jSONObject.put("dashboards", (Object) jSONArray2);
            jSONArray.add(jSONObject);
        }
        return jSONArray.toJSONString();
    }

    public static String getDeviceItemName(int i) {
        int i2 = R.string.sport_tag_distance;
        int i3 = R.string.unit_hots;
        switch (i) {
            case IDevicePanelItem.DATA_TYPE_DISTANCE -> {
                i2 = R.string.sport_tag_distance;
                i3 = R.string.unit_km;
            }
            case IDevicePanelItem.DATA_TYPE_SPEED -> {
                i2 = R.string.sport_tag_speed;
                i3 = R.string.unit_km_h;
            }
            case IDevicePanelItem.DATA_TYPE_AVG_SPEED_TOTAL_TIME -> {
                i2 = R.string.sport_tag_avg_speed_total_time;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_AVG_SPEED -> {
                i2 = R.string.sport_tag_avg_speed;
                i3 = R.string.unit_km_h;
            }
            case IDevicePanelItem.DATA_TYPE_MAX_SPEED -> {
                i2 = R.string.sport_tag_max_speed;
                i3 = R.string.unit_km_h;
            }
            case IDevicePanelItem.DATA_TYPE_PACE -> {
                i2 = R.string.sport_tag_pace;
                i3 = R.string.unit_min_km;
            }
            case IDevicePanelItem.DATA_TYPE_MAX_PACE -> {
                i2 = R.string.sport_tag_max_pace;
                i3 = R.string.unit_min_km;
            }
            case IDevicePanelItem.DATA_TYPE_AVG_PACE -> {
                i2 = R.string.sport_tag_avg_pace;
                i3 = R.string.unit_min_km;
            }
            case IDevicePanelItem.DATA_TYPE_TOTAL_DURATION -> {
                i2 = R.string.sport_tag_duration_total;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_DURATION -> {
                i2 = R.string.sport_tag_duration;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_PAUSE_DURATION -> {
                i2 = R.string.sport_tag_pause_time;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_ALTITUDE -> {
                i2 = R.string.sport_tag_altitude;
                i3 = R.string.unit_m;
            }
            case IDevicePanelItem.DATA_TYPE_MAX_ALTITUDE -> {
                i2 = R.string.sport_tag_max_altitude;
                i3 = R.string.unit_m;
            }
            case IDevicePanelItem.DATA_TYPE_MIN_ALTITUDE -> {
                i2 = R.string.sport_tag_min_altitude;
                i3 = R.string.unit_m;
            }
            case IDevicePanelItem.DATA_TYPE_ELEVATION_GAIN -> {
                i2 = R.string.sport_tag_elevation_gain;
                i3 = R.string.unit_m;
            }
            case IDevicePanelItem.DATA_TYPE_ELEVATION_LOSS -> {
                i2 = R.string.sport_tag_elevation_lost;
                i3 = R.string.unit_m;
            }
            case IDevicePanelItem.DATA_TYPE_GRADIENT -> {
                i2 = R.string.sport_tag_gradient;
                i3 = R.string.unit_percent;
            }
            case IDevicePanelItem.DATA_TYPE_CALORIE -> {
                i2 = R.string.sport_tag_calorie;
                i3 = R.string.unit_kcal;
            }
            case IDevicePanelItem.DATA_TYPE_HEART_RATE -> {
                i2 = R.string.sport_tag_heartrate;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_AVG_HEART_RATE -> {
                i2 = R.string.sport_tag_avg_hr;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_MAX_HEART_RATE -> {
                i2 = R.string.sport_tag_max_hr;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_CADENCE -> {
                i2 = R.string.sport_tag_cadence;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_AVG_CADENCE -> {
                i2 = R.string.sport_tag_avg_cadence;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_MAX_CADENCE -> {
                i2 = R.string.sport_tag_max_cadence;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_POWER -> {
                i2 = R.string.sport_tag_power;
                i3 = R.string.unit_power;
            }
            case IDevicePanelItem.DATA_TYPE_MAX_POWER -> {
                i2 = R.string.sport_tag_max_power;
                i3 = R.string.unit_power;
            }
            case IDevicePanelItem.DATA_TYPE_POWER_3_SECONDS -> {
                i2 = R.string.sport_tag_power_3_seconds;
                i3 = R.string.unit_power;
            }
            case IDevicePanelItem.DATA_TYPE_POWER_10_SECONDS -> {
                i2 = R.string.sport_tag_power_10_seconds;
                i3 = R.string.unit_power;
            }
            case IDevicePanelItem.DATA_TYPE_POWER_30_SECONDS -> {
                i2 = R.string.sport_tag_power_30_seconds;
                i3 = R.string.unit_power;
            }
            case IDevicePanelItem.DATA_TYPE_CURRENT_AZIMUTH -> {
                i2 = R.string.sport_tag_current_azimuth;
                i3 = 0;
            }
            case IDevicePanelItem.DATA_TYPE_TEMPERATURE ->
                    i2 = R.string.sport_tag_current_temperature;
            case IDevicePanelItem.DATA_TYPE_MAX_TEMPERATURE ->
                    i2 = R.string.sport_tag_max_temperature;
            case IDevicePanelItem.DATA_TYPE_MIN_TEMPERATURE ->
                    i2 = R.string.sport_tag_min_temperature;
            default -> {
                i2 = R.string.sport_tag_distance;
                i3 = 0;
            }
        }
        String name = App.getContext().getString(i2);
        if (i3 != 0) {
            name += "(" + App.getContext().getString(i3) + ")";
        }
        return name;
    }

    public static String resetDeviceDefault(int dataType) {
        String value;
        switch (dataType) {
            case IDevicePanelItem.DATA_TYPE_DISTANCE, IDevicePanelItem.DATA_TYPE_SPEED,
                 IDevicePanelItem.DATA_TYPE_AVG_SPEED, IDevicePanelItem.DATA_TYPE_MAX_SPEED,
                 IDevicePanelItem.DATA_TYPE_AVG_SPEED_TOTAL_TIME -> value = DEFAULT_FLOAT_2;
            case IDevicePanelItem.DATA_TYPE_DURATION, IDevicePanelItem.DATA_TYPE_TOTAL_DURATION,
                 IDevicePanelItem.DATA_TYPE_PAUSE_DURATION -> value = DEFAULT_TIME;
            case IDevicePanelItem.DATA_TYPE_PACE, IDevicePanelItem.DATA_TYPE_AVG_PACE,
                 IDevicePanelItem.DATA_TYPE_MAX_PACE -> value = DEFAULT_PACE;
            case IDevicePanelItem.DATA_TYPE_ALTITUDE, IDevicePanelItem.DATA_TYPE_GRADIENT,
                 IDevicePanelItem.DATA_TYPE_ELEVATION_GAIN, IDevicePanelItem.DATA_TYPE_MAX_ALTITUDE,
                 IDevicePanelItem.DATA_TYPE_MIN_ALTITUDE,
                 IDevicePanelItem.DATA_TYPE_ELEVATION_LOSS -> value = DEFAULT_FLOAT_1;
            case IDevicePanelItem.DATA_TYPE_CURRENT_AZIMUTH ->
                    value = HelpFormatter.DEFAULT_OPT_PREFIX;
            default -> value = "0";
        }
        return value;
    }
}